package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.mapper.CourseMarketMapper;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.service.CourseBaseInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Date:2023/6/23
 * Author:丐版小杨哥
 * Description:
 */
@Service
public class CourseBaseInfoServiceImpl implements CourseBaseInfoService {
    @Autowired
    private CourseBaseMapper courseBaseMapper;

    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private CourseCategoryMapper courseCategoryMapper;
    /**
     * 课程分页查询
     * @param pageParams
     * @param queryCourseParamsDto
     * @return
     */
    @Override
    public PageResult<CourseBase> queryCourseBaseList(PageParams pageParams,QueryCourseParamsDto queryCourseParamsDto) {
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        Page<CourseBase> page = new Page<>(pageParams.getPageNo(),pageParams.getPageSize());
        wrapper.like(StringUtils.isNotEmpty(queryCourseParamsDto.getCourseName()),CourseBase::getName,queryCourseParamsDto.getCourseName());
        wrapper.eq(StringUtils.isNotEmpty(queryCourseParamsDto.getAuditStatus()), CourseBase::getAuditStatus,queryCourseParamsDto.getAuditStatus());
        Page<CourseBase> pageResult = courseBaseMapper.selectPage(page, wrapper);
        List<CourseBase> list = pageResult.getRecords();
        long total = pageResult.getTotal();
        PageResult<CourseBase> result = new PageResult<CourseBase>(list,total,pageParams.getPageNo(),pageParams.getPageSize());
        return result;
    }

    /**
     * 新增课程信息
     * @param companyId
     * @param addCourseDto
     * @return
     */
    @Transactional
    public CourseBaseInfoDto createCourse(Long companyId,AddCourseDto addCourseDto){
        CourseBase courseBase = new CourseBase();
        BeanUtils.copyProperties(addCourseDto,courseBase);

        courseBase.setCompanyId(companyId);
        courseBase.setCreateDate(LocalDateTime.now());
        courseBase.setStatus("203001");
        courseBase.setAuditStatus("202001");
        int insert = courseBaseMapper.insert(courseBase);
        if (insert<=0){
            throw new RuntimeException("课程插入失败");
        }
        CourseMarket courseMarket = new CourseMarket();
        BeanUtils.copyProperties(addCourseDto,courseMarket);
        courseMarket.setId(courseBase.getId());
        int i = addCourseMarket(courseMarket);
        if (i<=0){
            throw new RuntimeException("课程插入失败");
        }
        CourseBaseInfoDto courseBaseInfoDto = getCourseBaseInfoDto(companyId);
        return courseBaseInfoDto;
    }

    /**
     * 根据id查询课程信息
     * @param baseAndMarketId
     * @return
     */
    public CourseBaseInfoDto getCourseBaseInfoDto(Long baseAndMarketId){
        CourseBase courseBase = courseBaseMapper.selectById(baseAndMarketId);
        if (courseBase==null){
            return null;
        }
        CourseMarket courseMarket = courseMarketMapper.selectById(baseAndMarketId);
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        BeanUtils.copyProperties(courseBase,courseBaseInfoDto);
        BeanUtils.copyProperties(courseMarket,courseBaseInfoDto);

        CourseCategory courseCategoryMt = courseCategoryMapper.selectById(courseBase.getMt());
        CourseCategory courseCategorySt = courseCategoryMapper.selectById(courseBase.getSt());
        courseBaseInfoDto.setMtName(courseCategoryMt.getName());
        courseBaseInfoDto.setStName(courseCategorySt.getName());
        return courseBaseInfoDto;
    }

    @Override
    @Transactional
    public CourseBaseInfoDto updateCourseBase(Long companyId,EditCourseDto editCourseDto) {
        Long id = editCourseDto.getId();
        CourseBase courseBase = courseBaseMapper.selectById(id);
        if (courseBase==null){
            throw new XueChengPlusException("课程不存在您无法修改");
        }
        Long companyIdTarget = courseBase.getCompanyId();
        if (!companyId.equals(companyIdTarget)){
            throw new XueChengPlusException("您不能修改其他机构的课程");
        }
        CourseBase courseBaseNew = new CourseBase();
        BeanUtils.copyProperties(editCourseDto,courseBaseNew);
        courseBaseNew.setChangeDate(LocalDateTime.now());

        CourseMarket courseMarket = new CourseMarket();
        BeanUtils.copyProperties(editCourseDto,courseMarket);
        int resultCourseBase = courseBaseMapper.updateById(courseBaseNew);
        int resultCourseMarket = courseMarketMapper.updateById(courseMarket);
        if(resultCourseBase<=0||resultCourseMarket<=0){
            throw new XueChengPlusException("课程修改失败");
        }
        return getCourseBaseInfoDto(id);
    }

    //增加营销策略
    private int addCourseMarket(CourseMarket courseMarket){
        //todo:数据校验

        CourseMarket courseMarketFromDB = courseMarketMapper.selectById(courseMarket.getId());
        if (courseMarketFromDB!=null){
            return courseMarketMapper.updateById(courseMarket);
        }else {
            return courseMarketMapper.insert(courseMarket);
        }
    }
}